import java.util.ArrayList;
import java.util.Arrays;

import java.util.Scanner;

import net.turkishpt.DictionaryValidator;
import net.turkishpt.FrequencyComparator;
import net.turkishpt.PhoneKey;
import net.turkishpt.PredictionEngine;

/**
 * It is the main driver for the T9-styled 
 * predictive text library for Turkish.
 *   
 * @author alp
 * {@link }
 * 
 */
public class MainDriver {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		
		System.out.println("Turkish T9(tm)-Styled Predictive Text Powered by Zemberek");
		System.out.println("*********************************************************");
		
		ArrayList<Integer> pressedKeys = new ArrayList<Integer>(); 
		int temp;
		do{
			System.out.print("Enter a Phone Key (1-9) and 0 to exit: ");
			temp = scan.nextInt();
			
			if(temp <= 0) break;
			
			if(temp>0 && temp<=9){
				pressedKeys.add(temp);
			} else {
				System.out.println("  Invalid number.Try again.");
			}
		}while(true);
		
		PhoneKey[] keyStrokes = new PhoneKey[pressedKeys.size()];
		
		for (int i = 0; i < keyStrokes.length; i++){
			keyStrokes[i] = PhoneKey.getPhoneKey(pressedKeys.get(i));
		}
		
		long start = System.nanoTime();
		
		ArrayList<String> combinations = PredictionEngine.getFilteredCombinations(keyStrokes, Integer.MAX_VALUE);
		ArrayList<String> results = DictionaryValidator.validateAll(combinations);
		
		
		for(String word : combinations)
			System.err.print("\""+word+"\" \n ");
		
		System.out.println(combinations.size()+" combinations will be tested.");
		
		combinations = null;
		System.gc(); // request a garbage collection.
		
		System.out.println("\n"+results.size()+" combinations found.");
		
		String[] frequencySortedResults = PredictionEngine.sortResultsByFrequency(results);
		
		for(String word : frequencySortedResults)
				System.out.print("\""+word+"\" ");

		
		long end = System.nanoTime();
		
		System.out.println("\nTook "+ (end-start)*1e-9 +" s.");
	}
	
}
